﻿using System;
using System.Reflection;
using System.Resources;

namespace TemplateFoundation.Handlers.DynamicImageProcessor
{
    /// <summary>
    /// Default configuration settings for the Dynamic Image Processor
    /// </summary>
    /// <remarks>This is used if no configuration has been added to web.config</remarks>
    public static class ProcessorDefaultConfig
    {
        /// <summary>
        /// Gets whether an "Image not found" image should be used for invalid image paths
        /// </summary>
        /// <seealso cref="NotFoundImage"/>
        public static Boolean EnableImageNotFound
        {
            get
            {
                return GetSettingBoolValue("EnableImageNotFound");
            }
        }

        /// <summary>
        /// Gets the default MIME type to use when returning images to the response stream
        /// </summary>
        public static string DefaultMimeType
        {
            get
            {
                return GetSettingStringValue("DefaultMimeType");
            }
        }

        /// <summary>
        /// Gets whether disk cache is enabled
        /// </summary>
        public static bool CacheEnabled
        {
            get
            {
                return GetSettingBoolValue("CacheEnabled");
            }
        }

        /// <summary>
        /// Gets the directory used for the disk cache
        /// </summary>
        /// <seealso cref="CacheEnabled"/>
        public static string CacheDirectory
        {
            get
            {
                return GetSettingStringValue("CacheDirectory");
            }
        }

        /// <summary>
        /// Gets the image to display if the requested image couldn't be found
        /// </summary>
        /// <seealso cref="EnableImageNotFound"/>
        public static string NotFoundImage
        {
            get
            {
                return GetSettingStringValue("NotFoundImage");
            }
        }

        /// <summary>
        /// Gets whether high quality rendering is used
        /// </summary>
        /// <seealso cref="EnableImageNotFound"/>
        public static bool HighQuality
        {
            get
            {
                return bool.Parse(GetSettingStringValue("HighQuality"));
            }
        }

        static string GetSettingStringValue(string name)
        {
            var manager = new ResourceManager("TemplateFoundation.Handlers.DynamicImageProcessor.ProcessorSettings", Assembly.GetExecutingAssembly());

            return manager.GetString(name);
        }

        static bool GetSettingBoolValue(string name)
        {
            var stringValue = GetSettingStringValue(name);

            bool value = false;

            bool.TryParse(stringValue, out value);

            return value;
        }
    }
}
